/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.Dao;

import com.lcfgly.DatabaseFactory.DataFactoryInterface;
import com.lcfgly.Interface.GetCacheMark;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

/**
 * 产品信息处理
 * @author lcfgly
 */
@Repository
public class Product implements Serializable{
    
//     private final String table="minpin_allshop";
    @Autowired
        @Qualifier("databaseshoprice")
    private DataFactoryInterface dataFactory;
    
    /**
     * 获取产品栏目
     * @return
     * @throws SQLException 
     */
    @GetCacheMark(value = "shopArctype",time=86400)
    public List GetArctype() throws SQLException{
        String sql="SELECT id,name FROM minpin_arctype WHERE model_id=2";
        return dataFactory.rowsWithOutObject(sql);
    }
    
    /**
     * 获取栏目信息
     * @param typeid
     * @return
     * @throws SQLException 
     */
    @GetCacheMark(value="shoptypeinfo_${0}")
    public Map GetTypeInfo(int typeid) throws SQLException{
        String sql="SELECT id,name,description FROM minpin_arctype WHERE id=?";
        return dataFactory.oneWithOutObject(sql, typeid);
    }
    
    /**
     * 返回产品信息
     * @param typeid
     * @param id
     * @return
     * @throws SQLException 
     */
    @GetCacheMark(value = "shopproductinfo_${0}_${1}")
    public Map GetProductInfo(int typeid,int id) throws SQLException{
        String sql="select minpin_allshop.*,addtable from minpin_allshop left join minpin_arctype on typeid=minpin_arctype.id where minpin_allshop.id=? AND minpin_arctype.id=?";
        Map result=dataFactory.oneWithOutObject(sql, id,typeid);
        String addtable=(String)result.get("addtable");
        sql="SELECT * FROM minpin_"+addtable+" WHERE aid=?";
        result.putAll(dataFactory.oneWithOutObject(sql, id));
        return result;
    }
    
    /**
     * 返回产品属性信息
     * @param addtable
     * @return
     * @throws SQLException 
     */
    @GetCacheMark(value = "shopProductProperty_${0}",time = 86400)
    public List GetProductProperty(String addtable)throws SQLException{
        String sql="SELECT minpin_modeltype.name,tablename FROM minpin_modeltype LEFT JOIN minpin_model ON minpin_model.id=minpin_modeltype.model_id WHERE addtable=? ";
        return dataFactory.rowsWithOutObject(sql, addtable);
    }
    
    /**
     * 获取产品图片
     * @param id
     * @param addtable
     * @return
     * @throws SQLException 
     */
    @GetCacheMark(value="shopProductImgs_${0}_${1}")
    public List GetProductImgs(int id,String addtable) throws SQLException{
        String sql="SELECT thumb,imgurl FROM minpin_imgs WHERE aid=? AND addtable=? ORDER BY short desc limit 6";
        return dataFactory.rowsWithOutObject(sql, id, addtable);
    }
    
    /**
     * 获取报价产品数
     * @param id
     * @return
     */
    public int getproductcount(int id){
        try{
            String sql="SELECT count(1) FROM shop_shopinfo WHERE uid = ?";
            dataFactory.Getrowsnum(sql, id);
            return -1;
        }catch(SQLException e){
            String sql="SELECT count(1) FROM shop_product WHERE shopid = ?";
            try {
                return dataFactory.Getrowsnum(sql, id);
            } catch (SQLException ex) {
                return 100;
            }
        }
    }
    
}
